/*
* 公共执行http请求
* */
import api from './api'
import request from './request'
import {codeError} from './helper'
import {jsLink, showToast, getDistance, indexOfSmallest} from './public'

const {appId, sign, cardType} = global.lineConfig.alipayCardInfo

const SUCCESS_CODE = '20000'

/*
* 成功的code
* */
export function getSuccessCode() {
  return SUCCESS_CODE
}

/*
* 根据token 获取uid
* @params auth_code
* @params cb
* */
export async function getUser({auth_code, cb}) {
  const {msg, data} = await request.apiGet(api.getAlipayUid, {
    cardType: cardType,
    appId: appId,
    sign: sign,
    auth_code: auth_code
  })
  sessionStorage.setItem('isClose', 'yes')
  if (msg && msg.code === SUCCESS_CODE) cb(data)
  else codeError()
}

/*
* 根据searchVal 获取线路信息
* */
export async function getLine({val, cb}) {
  const {resultCode, data} = await request.apiGet(api.lineQuery, {
    keyword: val
  })
  if (resultCode && resultCode === '1') {
    cb(data.lines)
  } else {
    codeError()
  }
}

/*
* 根据线路基本信息获取线路详细信息,并做实时数据处理
* */
export async function getDetail({lineId, latitude, longitude, clickStopName, clickStopOrder, cb}) {
  const {resultCode, data} = await request.apiGet(`${api.lineDetail}${lineId}.do`, {})
  if (resultCode && resultCode === '1') {
    data.isFavorite = false
    if (data.stops) {
      data.stops.forEach((v, i) => {
        v.nearest = false   // 地位判断识别标识，离用户最近并且距离小于500米
        v.distance = getDistance(latitude, longitude, v.lat, v.lng)    // 根据经纬度计算出，站点距离用户的距离
        v.isChoice = false // 用户选择标识，识别用户是否选择了该站点
        v.busComming = false // 站点有车辆信息，并且未到站
        v.busArrive = false // 站点有车辆信息，并且已到站
        v.isBeforeCurrentChoiceNode = false
        if (v.stopName === clickStopName&&v.stopOrder===clickStopOrder) {
          v.isChoice = true
        }
      })
      let arr = indexOfSmallest(data.stops, data.buses)
      cb(data, arr)
    } else {
      showToast('未查到该线路，请重新选择！')
      jsLink('href', '#/lineQuery')
    }
  } else {
    codeError()
  }
}

/*
* 收藏线路信息
* */
export async function lineCollect({url, lineId, lineNo, direction, clickStopName, cb}) {
  const {resultCode, data} = await request.apiPost(api[url], {
    userId: sessionStorage.getItem('userId'),
    lineId: lineId,
    lineNo: lineNo,
    direction: direction,
    clickStopName: clickStopName
  })
  if (resultCode && resultCode === '1') {
    cb()
  } else {
    codeError()
  }
}

/*
*获取用户线路设置信息
* */
export async function lineGet({cb}) {
  const {resultCode, data} = await request.apiPost(api.lineGet, {
    userId: sessionStorage.getItem('userId')
  })
  if (resultCode && resultCode === '1') {
    cb(data)
  } else {
    codeError()
  }
}

/*
* 设置用户线路提醒
* */
export async function lineSet({lineId, stopId, time, loop, cb}) {
  const {resultCode} = await request.apiPost(api.lineSet, {
    userId: sessionStorage.getItem('userId'),
    lineId: lineId,
    stopId: stopId,
    time: time,
    loop: loop
  })
  if (resultCode && resultCode === '1') {
    cb()
  } else {
    codeError()
  }
}

/*
* 获取用户附近站点信息
* */
export async function nearSite({latitude, longitude, cb}) {
  const {resultCode, data} = await request.apiGet(`${api.nearSite}${longitude}/${latitude}.do`, {
    _pageIndex: 0,
    _pageSize: 5
  })
  if (resultCode && resultCode === '1') {
    cb(data.rows)
  } else {
    codeError()
  }
}


/*
* 获取用户线路收藏信息
* */
export async function myCollect({cb}) {
  const {resultCode, data} = await request.apiPost(api.myGet, {
    userId: sessionStorage.getItem('userId')
  })
  if (resultCode && resultCode === '1') {
    cb(data)
  } else {
    codeError()
  }
}

/*
* 取消用户线路收藏信息
* */
export async function removeCollect({lineId, lineNo, direction, cb}) {
  const {resultCode} = await request.apiPost(api.myMove, {
    userId: sessionStorage.getItem('userId'),
    lineId: lineId,
    lineNo: lineNo,
    direction: direction
  })
  if (resultCode && resultCode === '1') {
    cb()
  } else {
    codeError()
  }
}

/*
* 取消用户线路收藏信息
* */
export async function addCollect({lineId, lineNo, direction, cb}) {
  const {resultCode} = await request.apiPost(api.mySave, {
    userId: sessionStorage.getItem('userId'),
    lineId: lineId,
    lineNo: lineNo,
    direction: direction
  })
  if (resultCode && resultCode === '1') {
    cb()
  } else {
    codeError()
  }
}

/*
* 获取线路站点信息
* */
export async function getStopInfo({lineId, cb}) {
  const {resultCode, data} = await request.apiGet(`${api.lineDetail}${lineId}.do`, {})
  if (resultCode && resultCode === '1') {
    cb(data)
  } else {
    codeError()
  }
}
